<?php

namespace app\common\model\banxi\trainer;

use app\common\enums\core\AuditStateEnum;
use app\common\enums\SexEnum;
use app\common\enums\TrainerAuditTypeEnum;
use app\common\enums\TrainerCheckEnum;
use app\common\enums\TrainerLevelEnum;
use app\common\enums\TrainerPracticeEnum;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\model\common\BaseModel;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 教练
 */
class TrainerAudit extends BaseModel
{

    use SoftDelete;

    protected $name = 'banxi_trainer_audit';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'applyStateCn',
        'auditTypeCn',
        'checkCn',
        'levelCn',
        'practiceCn',
        'sexCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'trainerId' => 'string',
        'title' => 'string',
        'auditUserId' => 'string',
        'isPublic' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------


    public function getApplyStateCnAttr($value, $data)
    {
        $value = $value ?: ($data['applyState'] ?? '');
        return (new AuditStateEnum())->getDescription($value) ?? '';
    }

    public function getAuditTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['auditType'] ?? '');
        return (new TrainerAuditTypeEnum())->getDescription($value) ?? '';

    }

    public function getCheckCnAttr($value, $data)
    {
        $value = $value ?: ($data['check'] ?? '');
        return (new TrainerCheckEnum())->getDescription($value) ?? '';

    }

    public function getLevelCnAttr($value, $data)
    {
        $value = $value ?: ($data['level'] ?? '');
        return (new TrainerLevelEnum())->getDescription($value) ?? '';

    }

    public function getPracticeCnAttr($value, $data)
    {
        $value = $value ?: ($data['practice'] ?? '');
        return (new TrainerPracticeEnum())->getDescription($value) ?? '';

    }


    public function getSexCnAttr($value, $data)
    {
        $value = $value ?: ($data['sex'] ?? '');
        return (new SexEnum())->getDescription($value) ?? '';

    }


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                      ｜
    //-----------------------------------------------------------------------------------
    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'organizationBaseInfo' => function ($query) {
                $query->field('id,name,managerName');
            },
            'titleInfo'
        ])->where($where)
            ->order(['id' => 'desc', 'updateTime' => 'desc'])
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ])->each(function ($item) {
                if ($item['organizationId'] == 0) {
                    $organizationModel = new OrganizationModel();
                    $item->organizationBaseInfo = $organizationModel->getOrganizationInfo();
                }
            });
    }

    public function getInfo(array $where): array|Model|null
    {
        $info = $this->with([
            'organizationBaseInfo' => function ($query) {
                $query->field('id,name,managerName');
            },
            'titleInfo'])->where($where)->find();
        if (!$info) {
            return [];
        }
        if ($info['organizationId'] == 0) {
            $organization = new OrganizationModel();
            $info['organizationBaseInfo'] = $organization->getOrganizationInfo();
        }
        return $info;
    }

    public function del(array $ids)
    {
        return self::destroy(function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        });

    }


    public function updateAuditById($openApplyId, $auditUserId, $auditUserName, $result, $auditReason = '')
    {
        return $this->update([
            'auditUserId' => $auditUserId,
            'auditUserName' => $auditUserName,
            'applyState' => $result ? AuditStateEnum::E2 : AuditStateEnum::E3,
            'auditReason' => $auditReason
        ], ['id' => $openApplyId, 'applyState' => AuditStateEnum::E1]);
    }
    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function organizationBaseInfo()
    {
        return $this->hasOne(OrganizationModel::class, 'id', 'organizationId');
    }

    public function titleInfo()
    {
        return $this->hasOne(TrainerTitle::class, 'id', 'title');
    }
}